package com.atwork.wuhua.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.CommunityAdapter;
import com.atwork.wuhua.base.BaseFragment;
import com.atwork.wuhua.bean.CommunityBean;
import com.atwork.wuhua.ui.activity.PersonInfoActivity;
import com.atwork.wuhua.ui.activity.PostActivity;
import com.atwork.wuhua.ui.activity.PostDetailActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.UserUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 社区
 */
public class CommunityFragment extends BaseFragment {
    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_presen)
    TextView tvPresen;
    @BindView(R.id.img_presen_type)
    ImageView imgPresenType;
    @BindView(R.id.tv_region)
    TextView tvRegion;
    @BindView(R.id.img_region_type)
    ImageView imgRegionType;
    @BindView(R.id.rv_community)
    RecyclerView rvCommunity;
    Unbinder unbinder;


    private CommunityAdapter communityAdapter;

    private List<CommunityBean> communityBeans;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;

    }

    @Override
    public int intiLayout() {
        return R.layout.fragment_community;
    }

    @Override
    public void initView() {
        imgLeft.setVisibility(View.GONE);
        tvRight.setVisibility(View.VISIBLE);
        tvTitle.setText("社区");
        tvRight.setText("发帖");

        communityBeans = new ArrayList<>();
        List<CommunityBean.DataBean> dataBeans = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            dataBeans.clear();
            for (int j = 0; j < 9; j++) {
                CommunityBean.DataBean dataBean = new CommunityBean.DataBean();
                dataBean.setThumb("http://im5.leaderhero.com/wallpaper/20140328/7afa04d2-4.jpg");
                dataBeans.add(dataBean);
            }
            communityBeans.add(new CommunityBean(i + "", dataBeans, i + ""));
        }
    }

    @Override
    public void initData() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCommunity.setLayoutManager(manager);
        communityAdapter = new CommunityAdapter(getActivity(), R.layout.item_community, communityBeans);
        rvCommunity.setAdapter(communityAdapter);

        communityAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = null;
                switch (view.getId()) {
                    case R.id.img_thumb:
                        LogUtils.e("个人详情");
                        intent = new Intent(getActivity(), PersonInfoActivity.class);
                        intent.putExtra(ConstantsMyself.INTENTID, position + "");
                        startActivity(intent);
                        break;
                    case R.id.tv_content:
                        LogUtils.e("内容");
                        intent = new Intent(getActivity(), PostDetailActivity.class);
                        intent.putExtra(ConstantsMyself.INTENTID, position + "");
                        startActivity(intent);
                        break;
                    case R.id.lly_zan:
                        LogUtils.e("点赞");
                        break;
                    case R.id.lly_comment:
                        LogUtils.e("评论");
                        break;
                    case R.id.lly_collection:
                        LogUtils.e("收藏");
                        break;
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.lly_level, R.id.lly_region, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lly_level:
                break;
            case R.id.lly_region:
                break;

            case R.id.tv_right:
                if (UserUtil.isLoginOrOpen(getActivity())) {
                    Intent intent = new Intent(getActivity(), PostActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    @OnClick(R.id.tv_right)
    public void onViewClicked() {
    }
}
